package com.uduew.sjd.utils

import android.Manifest
import android.content.Context
import android.net.ConnectivityManager
import android.os.Build
import android.telephony.SubscriptionManager
import android.telephony.TelephonyManager
import androidx.annotation.RequiresPermission
import com.uduew.sjd.api.SubmitDeviceInfoApi.ReqSubmitDeviceInfoBean.FreshnessBean

object SimInfoCollector {
    // 获取SIM卡信息
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    fun getSimInfo(context: Context): FreshnessBean {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        val subManager =
            context.getSystemService(Context.TELEPHONY_SUBSCRIPTION_SERVICE) as SubscriptionManager

        return FreshnessBean(
            getSimCountryCode(tm, subManager),
            getSimSerialNumber(tm),
            isSimActive(subManager),
            getSimOperatorCode(tm),
            getSimOperatorName(tm),
            isMobileDataEnabled(context),
            isMobileDataActive(context),
            getNetworkType(context)
        )
    }

    // 获取SIM卡国家代码
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    private fun getSimCountryCode(tm: TelephonyManager, subManager: SubscriptionManager): String {
        return subManager.activeSubscriptionInfoList?.firstOrNull()?.countryIso?.toUpperCase() ?: ""
    }

    // 获取SIM卡序列号
    private fun getSimSerialNumber(tm: TelephonyManager): String {
        return try {
            if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.O) {
                tm.getSimSerialNumber() ?: ""
            } else {
                @Suppress("DEPRECATION")
                tm.simSerialNumber ?: ""
            }
        } catch (e: SecurityException) {
            "PERMISSION_DENIED"
        }
    }

    // 检查SIM卡状态
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    private fun isSimActive(subManager: SubscriptionManager): Boolean {
        return subManager.activeSubscriptionInfoCount > 0
    }

    // 获取运营商代码
    private fun getSimOperatorCode(tm: TelephonyManager): String {
        return tm.simOperator ?: ""
    }

    // 获取运营商名称
    private fun getSimOperatorName(tm: TelephonyManager): String {
        return tm.simOperatorName ?: ""
    }

    // 是否启用移动数据
    private fun isMobileDataEnabled(context: Context): Boolean {
        return try {
            val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
            val method = cm.javaClass.getMethod("getMobileDataEnabled")
            method.invoke(cm) as Boolean
        } catch (e: Exception) {
            false
        }
    }

    // 是否正在使用移动数据
    private fun isMobileDataActive(context: Context): Boolean {
        val cm = context.getSystemService(Context.CONNECTIVITY_SERVICE) as ConnectivityManager
        val activeNetwork = cm.activeNetworkInfo
        return activeNetwork?.type == ConnectivityManager.TYPE_MOBILE && activeNetwork.isConnected
    }

    // 获取网络类型
    @RequiresPermission(Manifest.permission.READ_PHONE_STATE)
    private fun getNetworkType(context: Context): String {
        val tm = context.getSystemService(Context.TELEPHONY_SERVICE) as TelephonyManager
        return when {
            true -> {
                when (tm.dataNetworkType) {
                    TelephonyManager.NETWORK_TYPE_LTE -> "4G"
                    TelephonyManager.NETWORK_TYPE_NR -> "5G"
                    TelephonyManager.NETWORK_TYPE_HSPAP -> "3G"
                    else -> "UNKNOWN"
                }
            }

            else -> {
                @Suppress("DEPRECATION")
                when (tm.networkType) {
                    TelephonyManager.NETWORK_TYPE_LTE -> "4G"
                    TelephonyManager.NETWORK_TYPE_HSPAP -> "3G"
                    else -> "UNKNOWN"
                }
            }
        }
    }

    data class SimInfoBean(
        val lobo: String,
        val incretion: String,
        val supernumerary: Boolean,
        val tacharanite: String,
        val dualin: String,
        val uncountable: Boolean,
        val lumbermill: Boolean,
        val disseizin: String
    )
}
